package com.yisu.auction.api.web.controller.operatorBusiness;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yisu.auction.api.core.service.IMobileRechargeService;
import com.yisu.auction.api.core.service.ISystemConfigService;
import com.yisu.auction.api.domain.request.operatorBusiness.TelephoneChargeRequest;
import com.yisu.auction.api.domain.vo.configJsonVos.BalanceJsonVo;
import com.yisu.auction.api.domain.vo.configJsonVos.TrafficJsonVo;
import com.yisu.auction.api.domain.vo.indexPackageVos.SystemConfigVo;
import com.yisu.auction.api.domain.vo.mobileChargePackageVos.RechargeResponse;
import com.yisu.auction.api.web.controller.BaseController;
import com.yisu.auction.ewf.common.utils.MD5Util;
import com.yisu.auction.ewf.common.web.HttpWebClient;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.MobileRecharge;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/2
 */
@Controller
public class MobileChargeUtil111 extends BaseController {

    @Resource
    private IMobileRechargeService iMobileRechargeService;

    @Resource
    private ISystemConfigService iSystemConfigService;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private String interfaceUrl = "http://47.92.146.117:7071";
    private String accountname = "test";
    private String servicenumber;
    private String face;
    private String pkgtype;
    private String agentbillno;
    private String sign;
    private String passwword = "888888";

    public MobileChargeUtil111() {
    }

    public MobileChargeUtil111(String agentbillno) {
        this.agentbillno = agentbillno;
    }

    /**
     * 流量充值
     *
     * @return
     */
    public boolean FlowRecharge() {
        RechargeResponse rechargeResponse = new RechargeResponse();
        MobileRecharge mobileRecharge = iMobileRechargeService.getMobileRechargeByOrderNo(this.agentbillno);
        Map<String, String> params = Maps.newConcurrentMap();
        params.put("accountname", accountname);
        params.put("servicenumber", mobileRecharge.getMobile());
        params.put("face", String.valueOf(mobileRecharge.getFace()));
        params.put("agentbillno", this.agentbillno);
        String signStr = accountname + face + agentbillno + passwword;
        sign = MD5Util.encode(signStr);
        params.put("sign", signStr);
        String jsonResult = "";
        if (mobileRecharge.getRechargeType() == 2) {
            params.put("pkgtype", String.valueOf(mobileRecharge.getPkgType()));
            jsonResult = HttpWebClient.post(interfaceUrl + "/mobileflow/recharge", params);
        }
        if (mobileRecharge.getRechargeType() == 1) {
            jsonResult = HttpWebClient.post(interfaceUrl + "/recharge/index", params);
        }
        rechargeResponse = JSON.parseObject(jsonResult, RechargeResponse.class);
        if (rechargeResponse != null) {
            if (rechargeResponse.getErrorcode().equals("0")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取流量充值配置JSON
     *
     * @param id
     * @return
     */
    public TrafficJsonVo getTrafficJson(String id) {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("trafficJson");
        Gson gson = new Gson();
        Type type = new TypeToken<List<TrafficJsonVo>>() {
        }.getType();
        List<TrafficJsonVo> trafficJsonVos = gson.fromJson(systemConfigVo.getConfigValue(), type);
        for (TrafficJsonVo t : trafficJsonVos) {
            if (t.getId().equals(id)) {
                return t;
            }
        }
        return null;
    }
}
